package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.system.domain.SysType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFilter;
import com.ruoyi.system.service.ISysFilterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-09-05
 */
@Controller
@RequestMapping("/system/filter")
public class SysFilterController extends BaseController
{
    private String prefix = "system/filter";

    @Autowired
    private ISysFilterService sysFilterService;

    @RequiresPermissions("system:filter:view")
    @GetMapping()
    public String filter()
    {
        return prefix + "/filter";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:filter:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysFilter>list(SysFilter sysFilter)
    {
        List<SysFilter> list = sysFilterService.selectSysFilterList(sysFilter);
        return list;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:filter:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysFilter sysFilter)
    {
        List<SysFilter> list = sysFilterService.selectSysFilterList(sysFilter);
        ExcelUtil<SysFilter> util = new ExcelUtil<SysFilter>(SysFilter.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SysFilter sysType = sysFilterService.selectFilterById(parentId);
        mmap.put("dept", sysType);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:filter:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysFilter sysFilter)
    {
        return toAjax(sysFilterService.insertSysFilter(sysFilter));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:filter:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysFilter sysType = sysFilterService.selectFilterById(id);
        mmap.put("sysType", sysType);
        return prefix + "/edit";
    }
    /**
     * 选择类型树
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectTypeTree/{deptId}", "/selectTypeTree/{deptId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("deptId") Long deptId,
                                 @PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap)
    {
        mmap.put("dept", sysFilterService.selectSysFilterById(deptId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }
    /**
     * 加载分类列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId)
    {
        SysFilter dept = new SysFilter();
        dept.setExcludeId(excludeId);
        List<Ztree> ztrees = sysFilterService.selectDeptTreeExcludeChild(dept);

        return ztrees;
    }
    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:filter:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysFilter sysFilter)
    {
        return toAjax(sysFilterService.updateSysFilter(sysFilter));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:filter:remove")
    @Log(title = "删除分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId)
    {
        if (sysFilterService.selectDeptCount(deptId) > 0)
        {
            return AjaxResult.warn("存在下级分类,不允许删除");
        }
        if (sysFilterService.checkDeptExistUser(deptId))
        {
            return AjaxResult.warn("部门存在用户,不允许删除");
        }
        return toAjax(sysFilterService.deleteSysFilterById(deptId));
    }
}
